<?php

namespace App\Http\Controllers\Merchant;

use App\Http\Response;
use App\Imports\InvitationsImport;
use App\Invitation;
use App\Jobs\SendEmail;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpWord\IOFactory;

class InvitationController extends Controller
{
    use Response;

    public function upload_template(Request $request)
    {
        $lang=$request->get('lang');
        $file = $request->file('file');
        if ($file->isValid()) {
            $extension = $file->getClientOriginalExtension();
            if (in_array($extension, ['docx', 'doc'])) {
                $name = uniqid() . '.' . $extension;

                $file->move(public_path('template'), $name);
                $word = IOFactory::load(public_path('template/' . $name));
                $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($word, "HTML");
                $fileName = auth('merchant')->id() . '_'.$lang.".blade.php";
                if (file_exists(resource_path('views/template/') . $fileName)){
                    unlink(resource_path('views/template/') . $fileName);
                }
                $xmlWriter->save(resource_path('views/template/') . $fileName);
                $body=file_get_contents(resource_path('views/template/') . $fileName);
                $body=str_replace("%7B%7B",'{{',$body);
                $body=str_replace("%7D%7D",'}}',$body);
                $body=str_replace("%24",'%',$body);
                unlink(public_path('template/' . $name));
                unlink(resource_path('views/template/') . $fileName);
                file_put_contents(resource_path('views/template/') . $fileName,$body);
                return $this->message(200, trans('topic.upload success'));
            } else {
                return $this->message(505, trans('topic.extension error'));
            }
        } else {
            return $this->message(505, trans('topic.upload failed'));
        }
    }

    public function upload_user(Request $request)
    {
        $file = $request->file('invitation_file');
        $allowed_ext = ['xlsx', 'xls'];
        if (!in_array($file->getClientOriginalExtension(), $allowed_ext)) {
            return $this->message(505, trans('topic.extension error'));
        }
        $mch_id = auth('merchant')->id();
        $send_uid = 0;
        Excel::import(new InvitationsImport($mch_id, $send_uid), $request->file('invitation_file'));
        return $this->message(200, trans('topic.upload success'));
    }
    public function invite(Request $request)
    {
        $email=$request->get('email');
        $lang=$request->get('lang');
        $where=[
            ['mch_id','=',auth('merchant')->id()],
            ['email','=',$email]
        ];
        $find=Invitation::where($where)->first();
        if ($find){
            return $this->message(505,trans('topic.invite failed'));
        }
        $data=[
            'mch_id'=>auth('merchant')->id(),
            'send_uid'=>0,
            'email'=>$email,
            'lang'=>$lang
        ];
        $created=Invitation::create($data);
        if ($created){
            return $this->message(200,trans('topic.invite success'));
        }else{
            return $this->error(505,trans('topic.invite failed'));
        }
    }
}
